package me.ivps.android.servicedemo.fragment;

import me.ivps.android.servicedemo.MainApp;
import me.ivps.android.servicedemo.R;
import me.ivps.android.servicedemo.service.AnotherService;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.TextView;

public class AnotherFragment extends Fragment {
    
    private static final String TAG = "MainFragment";
    
    private View mBtnStart, mBtnStop;
    private TextView mTv;
    private ScrollView mScroll;
    
    private MainApp mApp;
    
    private Handler handler = new Handler();
    
    public static AnotherFragment newInstance() {
        AnotherFragment instance = new AnotherFragment();
        return instance;
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");
        mApp = (MainApp) getActivity().getApplication();
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
        getActivity().stopService(
                new Intent(getActivity(), AnotherService.class));
        mApp = null;
    }
    
    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG, "onResume");
        startUpdate();
        handler.postDelayed(new Runnable() {
            
            @Override
            public void run() {
                mTv.setText(mApp.getText());
                handler.postDelayed(this, 2000L);
            }
        }, 200L);
    }
    
    @Override
    public void onPause() {
        super.onPause();
        Log.d(TAG, "onPause");
        stopUpdate();
        handler.removeCallbacksAndMessages(null);
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        Log.d(TAG, "onCreateView");
        View rootView = inflater.inflate(R.layout.fragment_service_demo,
                container, false);
        initView(rootView);
        bindListener();
        return rootView;
    }
    
    private void startUpdate() {
        Intent intent = new Intent(getActivity(), AnotherService.class);
        intent.setAction(AnotherService.Action_Start);
        getActivity().startService(intent);
    }
    
    private void stopUpdate() {
        Intent intent = new Intent(getActivity(), AnotherService.class);
        intent.setAction(AnotherService.Action_Stop);
        getActivity().startService(intent);
    }
    
    private void initView(View rootView) {
        mBtnStart = rootView.findViewById(R.id.btn_start);
        mBtnStop = rootView.findViewById(R.id.btn_stop);
        mTv = (TextView) rootView.findViewById(R.id.tv);
        mScroll = (ScrollView) rootView.findViewById(R.id.scroll);
    }
    
    private void bindListener() {
        View.OnClickListener listener = new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.btn_start:
                        startUpdate();
                        break;
                    case R.id.btn_stop:
                        stopUpdate();
                        break;
                }
            }
        };
        mBtnStart.setOnClickListener(listener);
        mBtnStop.setOnClickListener(listener);
        mTv.addTextChangedListener(new TextWatcher() {
            
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                
            }
            
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                
            }
            
            @Override
            public void afterTextChanged(Editable s) {
                mScroll.invalidate();
            }
        });
    }
    
}